package com.zlm.app.emp.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.zlm.app.emp.service.UserEmpService;
import com.zlm.common.constants.CodeConstants;
import com.zlm.common.constants.UserConstants;
import com.zlm.common.dto.JsonResultDto;
import com.zlm.common.entity.UserTokenDO;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 个人版消息界面，与合伙人版业务重合，考虑拆出单独服务
 * @Author: duyufu
 * @Date: 2019/7/5 16:14
 */
@RestController
@RequestMapping(value = "/message")
public class MessageController extends CheckController {

    @Autowired
    private UserEmpService userEmpService;

    /**
     * 环信注册
     * @param token
     * @return
     */
    @GetMapping("/huanxinRegister")
    public JsonResultDto huanxinRegister(String token) {
        try {
            //判断token失效
            UserTokenDO userToken = checkToken(token);
            //环信注册
            JsonResultDto registerResult =  userEmpService.huanxinRegister(userToken.getUserId());
            checkJsonResultDto(registerResult);
            return success();
        } catch (Exception e) {
            e.printStackTrace();
            return fail(e.getMessage());
        }
    }

    /**
     * 环信添加好友
     * @param token
     * @param userHxId
     * @param friendHxId
     * @return
     */
    @GetMapping("/huanxinAddFriend")
    public JsonResultDto huanxinAddFriend(String token, String userHxId, String friendHxId) {
        try {
            if (StringUtils.isBlank(userHxId)||StringUtils.isBlank(friendHxId)) {
                throw new RuntimeException(CodeConstants.CODE_PARAMS);
            }
            checkToken(token);
            //找了么添加好友
            JsonResultDto result = userEmpService.addFriend(userHxId,friendHxId);
            checkJsonResultDto(result);
            return success();
        } catch (Exception e) {
            e.printStackTrace();
            return fail(e.getMessage());
        }
    }

    /**
     * 环信删除好友
     * @param token
     * @param userHxId
     * @param friendHxId
     * @return
     */
    @GetMapping("/huanxinDeleteFriend")
    public JsonResultDto huanxinDeleteFriend(String token, String userHxId, String friendHxId) {
        try {
            if (StringUtils.isBlank(userHxId)||StringUtils.isBlank(friendHxId)) {
                throw new RuntimeException(CodeConstants.CODE_PARAMS);
            }
            checkToken(token);
            //找了么删除好友
            JsonResultDto result = userEmpService.deleteFriend(userHxId,friendHxId);
            checkJsonResultDto(result);
            return success();
        } catch (RuntimeException e) {
            e.printStackTrace();
            return fail(e.getMessage());
        }
    }

    /**
     * 获取单个用户
     * @param token
     * @return
     */
    @GetMapping("/huanxinGetUser")
    public JsonResultDto huanxinGetUser(String token, String userHxId) {
        try {
            if (StringUtils.isBlank(userHxId)) {
                throw new RuntimeException(CodeConstants.CODE_PARAMS);
            }
            checkToken(token);
            JsonResultDto result = userEmpService.queryHxUser(userHxId);
            checkJsonResultDto(result);
            return success(result.getData());
        } catch (RuntimeException e) {
            e.printStackTrace();
            return fail(e.getMessage());
        }
    }

    /**
     * 获取环信好友列表
     * @param token
     * @return
     */
    @GetMapping("/huanxinFriendList")
    public JsonResultDto huanxinFriendList(String token, String userHxId) {
        try {
            if (StringUtils.isBlank(userHxId)) {
                throw new RuntimeException(CodeConstants.CODE_PARAMS);
            }
            checkToken(token);
            //找了么好友
            JsonResultDto result = userEmpService.queryFriendList(userHxId);
            checkJsonResultDto(result);
            return success(result.getData());
        } catch (RuntimeException e) {
            e.printStackTrace();
            return fail(e.getMessage());
        }
    }


    /**
     * 强制下线
     * @param token
     * @return
     */
    @GetMapping("/huanxinDisconnect")
    public JsonResultDto huanxinDisconnect(String token) {
        try {
            UserTokenDO userToken = checkToken(token);
            userEmpService.huanxinDisconnect(UserConstants.USER_TYPE_EMP_STRING.concat(userToken.getUserId().toString()));
            return success();
        } catch (RuntimeException e) {
            e.printStackTrace();
            return fail(e.getMessage());
        }
    }

    /**
     * 搜索好友
     * @param token
     * @param keyword
     * @return
     */
    @GetMapping("/searchFriend")
    public JsonResultDto searchFriend(String token,String keyword) {
        try {
            if (null == keyword) {
                throw new RuntimeException(CodeConstants.CODE_PARAMS);
            }
            checkToken(token);
            JsonResultDto result = userEmpService.searchFriend(keyword);
            checkJsonResultDto(result);
            return success(result.getData());
        } catch (RuntimeException e) {
            e.printStackTrace();
            return fail(e.getMessage());
        }
    }
}
